package com.situ.mall.consumer;

import java.util.LinkedList;

/**
 * 资源类（核心类）
 */
public class Panzi {
    //使用LinkedList模拟队列，在队列尾部添加，在头部移除
    private LinkedList<Cake> list = new LinkedList<>();

    public synchronized void putCake(Cake cake) {
        if (list.size() == 5) {//盘子最大放5个
            try {
                System.out.println("生产者线程 putCake wait");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //在队列尾部添加蛋糕
        list.addLast(cake);
        System.out.println("生产者线程 putCake notifyAll cake: " + cake);
        notifyAll();
    }

    public synchronized Cake getCake() {
        //先判断
        if (list.size() <= 0) {//盘子里面没有蛋糕
            try {
                System.out.println("消费者线程 getCake wait");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //队列头部删除蛋糕
        Cake cake = list.removeFirst();
        System.out.println("消费者线程 getCake notifyAll cake: " + cake);
        notifyAll();
        return cake;
    }
}
